import { formatTime } from '../../../../utils/jk/jk-tool';

export default {
	data() {
		return {
			unitList: ['时', '天', '月', '年', '产量(米)', '产量(kg)', '产量(吨)']
		};
	},
	methods: {
		transferCircleUnitName(e) {
			return this.unitList[e];
		},
		transferCircleUnit(item) {
			return item.cycleUnit || item.cycleUnit === 0 ? `${item.cycleMin}~${item.cycleMax}${this.unitList[item.cycleUnit]}` : '';
		},
		calNextTime() {
			const e = this.ruleForm.actualEndTime;
			if (e) {
				const filZero = n => n < 10 ? '0' + n : '' + n;
				if (this.ruleForm.cycleUnit === 0) {
					// 小时
					const timeStamp = Date.parse(e) + (this.ruleForm.cycleMin * 60 * 60 * 1000);
					this.$set(this.ruleForm, 'nextPlanTime', formatTime(timeStamp));
				} else if (this.ruleForm.cycleUnit === 1) {
					// 天
					const timeStamp = Date.parse(e) + (this.ruleForm.cycleMin * 24 * 60 * 60 * 1000);
					this.$set(this.ruleForm, 'nextPlanTime', formatTime(timeStamp));
				} else if (this.ruleForm.cycleUnit === 2) {
					// 月
					let currentYear = new Date(e).getFullYear();
					let currentMonth = new Date(e).getMonth() + 1;
					let currentDate = filZero(new Date(e).getDate());
					const currentHour = filZero(new Date(e).getHours());
					const currentMinute = filZero(new Date(e).getMinutes());
					const currentSecond = filZero(new Date(e).getSeconds());
					for (let i = 0; i < this.ruleForm.cycleMin; i++) {
						if (currentMonth === 12) {
							currentMonth = 1;
							currentYear += 1;
						} else {
							currentMonth++;
						}
					}
					if (currentDate > new Date(currentYear, currentMonth, 0).getDate()) {
						currentDate = new Date(currentYear, currentMonth, 0).getDate();
					}
					const timeStamp = `${currentYear}-${currentMonth}-${currentDate} ${currentHour}:${currentMinute}:${currentSecond}`;
					this.$set(this.ruleForm, 'nextPlanTime', timeStamp);
				} else if (this.ruleForm.cycleUnit === 3) {
					// 年
					let currentYear = new Date(e).getFullYear();
					const currentMonth = new Date(e).getMonth() + 1;
					let currentDate = filZero(new Date(e).getDate());
					const currentHour = filZero(new Date(e).getHours());
					const currentMinute = filZero(new Date(e).getMinutes());
					const currentSecond = filZero(new Date(e).getSeconds());
					for (let i = 0; i < this.ruleForm.cycleMin; i++) currentYear++;
					if (currentDate > new Date(currentYear, currentMonth, 0).getDate()) {
						currentDate = new Date(currentYear, currentMonth, 0).getDate();
					}
					const timeStamp = `${currentYear}-${currentMonth}-${currentDate} ${currentHour}:${currentMinute}:${currentSecond}`;
					this.$set(this.ruleForm, 'nextPlanTime', timeStamp);
				}
			} else {
				this.$set(this.ruleForm, 'nextPlanTime', '');
			}
		}
	}
};
